package com.fjec.market.controller;

import com.fjec.common.annotation.Json;
import com.fjec.common.protocol.JsonResult;
import com.fjec.market.service.IBizShoppingCartService;
import com.fjec.market.view.OrderItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 016.购物车表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-09-17
 */
@Slf4j
@RestController
@RequestMapping("/")
public class BizShoppingCartController {

    @Resource
    private IBizShoppingCartService service;

    @Resource(name = "phyOrderAmqpTemplate")
    private RabbitTemplate rabbitTemplate;

    /**
     * 加入购物车
     *
     * @param skuId
     * @param num
     * @return
     */
    @PostMapping("/y/shoppingcart/save")
    public JsonResult save(Long skuId, Integer num) {
        return service.save(skuId, num);
    }

    /**
     * 购物车商品列表
     *
     * @return
     */
    @GetMapping("/y/shoppingcart/list")
    public JsonResult getList() {
        return service.getList();
    }

    /**
     * 增加减少数量
     *
     * @param id
     * @param num
     * @return
     */
    @PostMapping("/y/shoppingcart/chgnum")
    public JsonResult changeNum(Long id, Integer num) {
        return service.changeNum(id, num);
    }

    /**
     * 逻辑删除购物车里的商品
     *
     * @param id
     * @return
     */
    @PostMapping("/y/shoppingcart/del")
    public JsonResult delete(Long id) {
        return service.delete(id);
    }

//    /**
//     * 提交订单
//     *
//     * @param addressName
//     * @param addressPhone
//     * @param address
//     * @param totalPrice
//     * @param productList
//     * @return
//     */
//    @PostMapping("y/shoppingcart/order/save")
//    public JsonResult saveOrder(String addressName, String addressPhone, String address,
//                                Integer totalPrice, Integer orderType, @Json List<OrderItem> productList) {
//
//
//        return null;
//    }

    @PostMapping("/sc/test")
    public String test() {
        //消息内容
        Map<String, String> map = new HashMap<>();
        map.put("message", "testing confire function");
        //设置自定义反馈消息
        String uuid = UUID.randomUUID().toString();
        log.info("消息唯一ID：" + uuid);
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(uuid);
        //并不存在名为“exchange-dog”的exchange
        rabbitTemplate.convertAndSend("exchange-dog", "dog", map, correlationData);
        return "ok";
    }

}
